% PURPOSE: Returns results for GMV portfolios for Table 8 of paper with characteristic based portfolios as the investment universe..
% INPUTS:
%  char_no : Number representing the set of characteristic based portfolios
%  to optimize over
%SampleStart = The start date for sample and for Galton learning
%SampleEnd   = The end date for the sample. Galton learning stops "post_window" E months earlier
%
%PARAMETERS:
%pre_window=The historical sample estimation window H
%post_window=The period of the subsequent realized window E
%learning_window=The window for Galton to pre-learn and others to skip
%stats = The statistics from the horserace requested for output and
%display. These are calculated in TradingStrategy.m
%
%OUTPUT:
%  table_out: horse race statistics
%  names: names of strategies included in the horse race

function [table_out, names] = Table_Char_Ports_GMV(char_no,SampleStart,SampleEnd)


    %clear
    %char_no = 1;
    % mean_shrink_setting = 0;
    % complete_port_setting = 0;
    filenames = {'ME_BTM.csv'; 'OP_INV.csv';'ME_BETA.csv';'ME_Prior.csv'};
    filename = (filenames{char_no});
    dataOut = csvread(filename,1);
    %dataOut = load(filename);


    data = GetSample();
    data.dataOut = dataOut;
    % data.dataOut(:,2:end) = dataOut(:,2:end)/100;
    data.dates = dataOut(:,1);

    data.SampleStart = SampleStart;
    data.SampleEnd   = SampleEnd;


    learning_window=120; %The window for Galton to pre-learn and others to skip
    pre_window=120; %The estimation window
    post_window=12; %The period of the subsequent realized window
    
    
    stats = {'sharpe'; 'activeShare';'turnover';...
        'min';'max';'mean_std_weights';'std_std_weights';...
        'neg_weights';
         };
 
    [ eret, T ] = data.getExcessReturnsFromFile();
  
    markowtizStratGMV = MarkowitzStrategyGMV;
    egStratGMV = EltonGruberStrategyGMV;
    lwCorrStratGMV = LedoitWolfCorrStrategyGMV;
  
    tradingStrategyArray = [   markowtizStratGMV egStratGMV lwCorrStratGMV];
    
    stock_ids = [1:size(eret,1)]'*ones([1,T]);
    hr = HorseRace;
    hr.learning_window=learning_window; 
    hr.pre_window=pre_window;
    hr.post_window=post_window;
    hr.rf = data.getRf();
    hr = hr.chooseHorses(tradingStrategyArray);
    hr = hr.runHorserace(eret,stock_ids);
    hr = hr.computeHorseraceResults();
    hr.outputPerformanceResults(stats);
    hr.outputPerformanceRiskResults();
    hr.outputRiskResults;
    
    table_out = hr.performanceRiskResults;
    names = hr.tradingStrategyNames;
 
end

